﻿using Newtonsoft.Json;
using System.Web.Mvc;

namespace QunProject.Web.Controllers
{
    /// <summary>
    /// 登录
    /// </summary>
    public class LoginController : BaseController
    {
        //登录
        public ActionResult Index(string type)
        {
            ViewBag.Type = type;
            if (LoginMember != null)
            {
                return RedirectToAction("Index", "Home");
            }
            else
            {
                return View();
            }
        }
        //注册
        public ActionResult Register()
        {
            if (LoginMember != null)
            {
                return RedirectToAction("Index", "Home");
            }
            else
            {
                return View();
            }
        }
        //找回
        public ActionResult Back()
        {
            if (LoginMember != null)
            {
                return RedirectToAction("Index", "Home");
            }
            else
            {
                return View();
            }
        }
        //注销登录
        public ActionResult Exit()
        {
            LoginMember = null;
            return RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// 异步注销登录
        /// </summary>
        /// <returns></returns>
        public JsonResult AjaxExit()
        {
            JsonConvert.DeserializeObject<Common.Result>(HttpPost("UserLoginOut"));
            LoginMember = null;
            return Json(1);
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="Phone">手机号码</param>
        /// <param name="Password">密码</param>
        /// <param name="SmsCode">短信验证码</param>
        /// <returns></returns>
        public JsonResult UserLoginResult(string phone, string passWord = "", string smsCode = "")
        {
            var paras = WeaveParameters<LoginController>("UserLoginResult");
            var result = JsonConvert.DeserializeObject<Common.Result>(HttpPost("UserLoginResult", paras));
            if (result.succ)
            {
                token = result.data.ToString();
            }
            return Json(result);
        }

        /// <summary>
        /// 发送注册短信
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public JsonResult SendInsertSms(string phone)
        {
            if (Cache.Get($"register_{phone}") != null)
                return Json(Result.Fail("短信发送频率过快，请稍后再试"));

            var code = Common.RanString.RandomNumber(6);
            var smsResult = SendSmsResult(phone, code, (int)Model.EnumHelper.SmsType.注册);
            if (smsResult.succ)
            {
                Cache.Set($"register_{phone}", code, 2);
                return Json(Result.Success());
            }
            else
            {
                return Json(Result.Fail(smsResult.msg));
            }
        }

        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="Phone">手机号码</param>
        /// <param name="PassWord">密码</param>
        /// <param name="SmsCode">短信验证码</param>
        /// <returns></returns>
        public JsonResult UserInsertResult(string phone, string passWord, string passWord2, string smsCode)
        {
            var paras = WeaveParameters<LoginController>("UserInsertResult");
            if (paras["passWord"] != paras["passWord2"])
                return Json(Result.Fail("两次输入的密码不一致"));

            var result = HttpPost("UserInsertResult", paras);
            return Json(result);
        }

        /// <summary>
        /// 发送找回密码短信
        /// </summary>
        /// <param name="phone">手机号码</param>
        /// <returns></returns>
        public JsonResult SendBackSms(string phone)
        {
            if (Cache.Get($"back_{phone}") != null)
                return Json(Result.Fail("短信发送频率过快，请稍后再试"));

            var code = Common.RanString.RandomNumber(6);
            var smsResult = SendSmsResult(phone, code, (int)Model.EnumHelper.SmsType.找回密码);
            if (smsResult.succ)
            {
                Cache.Set($"back_{phone}", code, 2);
                return Json(Result.Success());
            }
            else
            {
                return Json(Result.Fail(smsResult.msg));
            }
        }

        /// <summary>
        /// 用户找回密码
        /// </summary>
        /// <param name="phone">手机号码</param>
        /// <param name="passWord">密码</param>
        /// <param name="smsCode">短信验证码</param>
        /// <returns></returns>
        public JsonResult UserBackPasswordResult(string phone, string passWord, string passWord2, string smsCode)
        {
            var paras = WeaveParameters<LoginController>("UserBackPasswordResult");
            if (paras["passWord"] != paras["passWord2"])
                return Json(Result.Fail("两次输入的密码不一致"));

            var result = HttpPost("UserBackPasswordResult", paras);
            return Json(result);
        }
    }
}